package com.ruoyi.food.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResult.Type;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.food.object.TRectInfo;
import com.ruoyi.food.service.TRectInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 整改记录controller
 * 
 * @author mengcc
 *
 */
@Api(value = "TRectInfoController", tags = "整改记录controller")
@Controller
@RequestMapping("/rectInfo")
public class TRectInfoController extends BaseController {

	@Autowired
	private TRectInfoService tRectInfoService;

	private String prefix = "page/week/rectInfo";

	@GetMapping()
	public String rectInfo() {
		return prefix + "/rectInfo";
	}

	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 保存整改记录
	 * @param tRectInfo
	 * @return
	 */
	@RequiresPermissions("rectInfo:add")
	@Log(title = "整改记录管理", businessType = BusinessType.INSERT)
	@ApiOperation(value = "保存整改记录", notes = "")
    @ApiParam(name = "tRectInfo", value = "整改记录对象", required = true, type = "TRectInfo")
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult save(@Validated TRectInfo tRectInfo) {
		List<TRectInfo> tRectInfos = new ArrayList<>();
		if(tRectInfo.getEnterpriseName()==null||tRectInfo.getEnterpriseName().equals("")) {
			return new AjaxResult(Type.ERROR,"被检查单位不能为空");
		}else if(tRectInfo.getInspectUnit()==null||tRectInfo.getInspectUnit().equals("")) {
			return new AjaxResult(Type.ERROR,"检查单位不能为空");
		}else if(tRectInfo.getTikcetNo()==null||tRectInfo.getTikcetNo().equals("")) {
			return new AjaxResult(Type.ERROR,"检查记录单号不能为空");
		}else if(tRectInfo.getCheckTime()==null) {
			return new AjaxResult(Type.ERROR,"问题发现日期不能为空");
		}else if(tRectInfo.getFinishTime()==null) {
			return new AjaxResult(Type.ERROR,"整改完成时间不能为空");
		}
		tRectInfo.setIsValid(1);
		tRectInfos.add(tRectInfo);
		return toAjax(tRectInfoService.saveTRectInfo(tRectInfos));

	}

	/**
	 * 查询整改记录
	 * @param tRectInfo
	 * @return
	 */
	@RequiresPermissions("rectInfo:list")
	@ApiOperation(value = "查询整改记录", notes = "")
    @ApiParam(name = "tRectInfo", value = "整改记录对象", required = true, type = "TRectInfo")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo findTRectInfo(TRectInfo tRectInfo) {
		startPage();
		List<TRectInfo> list = new ArrayList<>();
		list = tRectInfoService.findTRectInfo(tRectInfo);
		return getDataTable(list);
	}
	
	/**
	 * 查询全部整改记录
	 * @return
	 */
	@ApiOperation("查询全部整改记录")
	@PostMapping("/listAll")
	@ResponseBody
	public TableDataInfo findAllTRectInfo() {
		List<TRectInfo> list = new ArrayList<>();
		list = tRectInfoService.findAllTRectInfo();
		return getDataTable(list);
	}

	@GetMapping("/update/{rectId}")
	public String update(@PathVariable("rectId") Long rectId, ModelMap mmap) {
		mmap.put("tRectInfo", tRectInfoService.findTRectInfoById(rectId));
		return prefix + "/update";
	}

	/**
	 * 修改整改记录
	 * @param tRectInfo
	 * @return
	 */
	@RequiresPermissions("rectInfo:update")
	@ApiOperation(value = "修改整改记录", notes = "")
    @ApiParam(name = "tRectInfo", value = "整改记录对象", required = true, type = "TRectInfo")
	@PostMapping("/update")
	@ResponseBody
	public AjaxResult updateRedBlack(@Validated TRectInfo tRectInfo) {
		return toAjax(tRectInfoService.updateTRectInfo(tRectInfo));

	}

	/**
	 * 逻辑删除整改记录
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("rectInfo:delete")
	@ApiOperation(value = "删除整改记录（逻辑删除）", notes = "")
    @ApiParam(name = "tRectInfo", value = "整改记录对象", required = true, type = "TRectInfo")
	@PostMapping("/delete")
	@ResponseBody
	public AjaxResult deleteRedBlack(Long ids) {
		if(ids==null) {
			return new AjaxResult(Type.ERROR,"整改记录id不能为空");
		}
		return toAjax(tRectInfoService.deleteTRectInfo(ids));
	}

}
